import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  base: './', // 打包的静态资源引用路径
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
      Components({
        resolvers: [ElementPlusResolver()],
      })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    // 新增的正向代理配置
    proxy: {
      // 关键配置：当请求路径以 /api 开头时，
      // 把它代理到 http://localhost:8080
      '/api': {
        target: 'http://192.168.1.4:8080',
        changeOrigin: true, // 需要改变源
        // 可选：如果你后端的真实API路径不包含 /api, 可以重写路径
        // rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
